package com.ttai.presenter.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.ttai.model.dao.DBHelper;
import com.ttai.model.net.ResponseInfo;
import com.ttai.model.net.TokenBean;
import com.ttai.presenter.base.BasePresenter;
import com.ttai.ui.activity.EnterPage;
import com.ttai.ui.activity.UserHomePage;
import com.ttai.ui.activity.VerifyMiYao;

import retrofit2.Call;

public class VerifyMiYaoPresenter extends BasePresenter{
    VerifyMiYao verifyMiYao;
    public int acount = 1;


    public VerifyMiYaoPresenter(VerifyMiYao verifyMiYao) {
        this.verifyMiYao = verifyMiYao;
    }

    @Override
    protected void showError(String message) {
        acount++;
        Toast.makeText(verifyMiYao,"密钥错误,错误三次将返回",Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void parseJson(JsonObject json) {
        Toast.makeText(verifyMiYao,"验证成功",Toast.LENGTH_SHORT).show();
        Gson gson = new Gson();
        TokenBean tokenBean = gson.fromJson(json,TokenBean.class);
        insertTokenDB(tokenBean.getToken());

//        recoverDB();
        verifyMiYao.finish();
        Intent intent = new Intent(verifyMiYao, UserHomePage.class);
        verifyMiYao.startActivity(intent);
    }

    private void insertTokenDB(String token) {
        DBHelper dbHelper = DBHelper.getInstance(verifyMiYao);
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("token",token);
        db.update("tokenacount",values, null, null);
    }

    public void verifyMiyao1(String cid,String key,String token){
        Call<ResponseInfo> verifyMiyaoToken = responseInfoApi.verifyMiyao( cid, key, token);
        verifyMiyaoToken.enqueue(new CallBackAdapter());
    }
}
